import { snippets } from "@/lib/generated/snippets";
import { Snippet } from "@/components/code";
import { Callout, Card, Cards, Steps, Tabs } from "nextra/components";
import UniversalTabs from "@/components/UniversalTabs";

# Enqueuing a Task Run (Fire and Forget)

> This example assumes we have a [task](./your-first-task.mdx) registered on a running [worker](./workers.mdx).

Another method of triggering a task in Hatchet is to _enqueue_ the task without waiting for it to complete, sometimes known as "fire and forget". This pattern is useful for tasks that take a long time to complete or are not critical to the immediate operation of your application.

Some example use cases for fire-and-forget style tasks might be:

1. Sending a shipping confirmation email to a user once their order has shipped. This is a truly async task, in the sense that the user is not necessarily using your application when it happens, and the part of the application triggering the task does not need to know the result of the work, just that it has been enqueued (assuming that it will complete, of course).
2. Triggering a machine learning model training job that can take minutes, hours, or even days to complete. Similarly to above, it's likely that no part of the application needs to wait on the result of this work, it just needs to "fire and forget" it - meaning that it needs to kick it off, and let it complete whenever it completes.

<UniversalTabs items={["Python", "Typescript", "Go"]}>
  <Tabs.Tab title="Python">

    If we have the following workflow:

<Snippet src={snippets.python.trigger_methods.workflow.define_a_task} />

    You can use your `Workflow` object to run a task and "forget" it by calling the `run_no_wait` method. This method enqueue a task run and return a `WorkflowRunRef`, a reference to that run, without waiting for the result.

<Snippet src={snippets.python.trigger_methods.workflow.sync} />

You can also `await` the result of `aio_run_no_wait`:

<Snippet src={snippets.python.trigger_methods.workflow.async} />

Note that the type of `input` here is a Pydantic model that matches the input schema of your task.

  </Tabs.Tab>
  <Tabs.Tab title="Typescript">
    You can use your `Workflow` object to run a workflow and "forget" it by calling the `run_no_wait` method. This method enqueue a workflow run and return a `WorkflowRunRef`, a reference to that run, without waiting for the result.
    <Snippet src={snippets.typescript.simple.enqueue.enqueuing_a_workflow_fire_and_forget} />
  </Tabs.Tab>
  <Tabs.Tab title="Go">
    You can use your `Workflow` object to run a workflow and "forget" it by calling the `RunNoWait` method. This method enqueue a workflow run and return a `WorkflowRunRef`, a reference to that run, without waiting for the result.

    <Snippet src={snippets.go.simple.main.running_a_task_without_waiting} />

  </Tabs.Tab>
</UniversalTabs>

## Subscribing to results from an enqueued task

Often it is useful to subscribe to the results of a task at a later time. The `run_no_wait` method returns a `WorkflowRunRef` object which includes a listener for the result of the task.

<UniversalTabs items={["Python", "Typescript", "Go"]}>
  <Tabs.Tab title="Python">

Use `ref.result()` to block until the result is available:

<Snippet src={snippets.python.trigger_methods.workflow.result_sync} />

or await `aio_result`:

<Snippet src={snippets.python.trigger_methods.workflow.result_async} />

  </Tabs.Tab>
  <Tabs.Tab title="Typescript">
    You can use your `Workflow` object to run a workflow and "forget" it by calling the `run_no_wait` method. This method enqueue a workflow run and return a `WorkflowRunRef`, a reference to that run, without waiting for the result.
    <Snippet src={snippets.typescript.simple.enqueue.subscribing_to_results} />
  </Tabs.Tab>
  <Tabs.Tab title="Go">
    You can use your `Workflow` object to run a workflow and "forget" it by calling the `RunNoWait` method. This method enqueue a workflow run and return a `WorkflowRunRef`, a reference to that run, without waiting for the result.

    <Snippet src={snippets.go.simple.main.subscribing_to_results} />

  </Tabs.Tab>
</UniversalTabs>

## Triggering Runs in the Hatchet Dashboard

In the Hatchet Dashboard, you can trigger and view runs for your tasks.

Navigate to "Task Runs" in the left sidebar and click "Trigger Run" at the top right.

You can specify run parameters such as Input, Additional Metadata, and the Scheduled Time.

![Create Scheduled Run](../../public/schedule-dash.gif)
